package com.bjfu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjfu.eduservice.entity.EduChapter;
import com.bjfu.eduservice.entity.EduVideo;
import com.bjfu.eduservice.entity.chapter.ChapterVo;
import com.bjfu.eduservice.entity.chapter.VideoVo;
import com.bjfu.eduservice.mapper.EduChapterMapper;
import com.bjfu.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.eduservice.service.EduVideoService;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Guli
 * @since 2020-07-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterListByCourseId(String courseId) {
        //1.查询章节数据，根据课程ID查询课程所有章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);
        //System.out.println(chapterList);

        //2.查询课程所有小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper();
        videoWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(videoWrapper);

        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //System.out.println(eduChapter);

            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    //System.out.println(eduVideo);
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            // 把小节列表对象放入章节的children中
            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    // 删除章节的方法
    @Override
    public void deleteChapter(String chapterId) {
        // 根据chapter章节id查询小节表，如果没有数据删除，要是有数据不删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        // 我们主要看有没有东西，不需要里面的数据，所以使用这个方法
        int count = videoService.count(wrapper);
        if(count > 0){
            throw new GuliException(20001,"还存在小节，不能删除此章节");
        }else {
            int delete = baseMapper.deleteById(chapterId);
            if(delete < 1){
                throw new GuliException(20001,"删除章节失败");
            }
        }

    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        Integer c = baseMapper.selectCount(wrapper);
        if(c == 0){
            return;
        }
        int count = baseMapper.delete(wrapper);
        if(count<1){
            throw new GuliException(20001,"删除章节失败");
        }
    }
}
